<?php 
function reg(){
	$arr=$_POST;
	$arr['password']=md5($_POST['password']);
	$arr['regTime']=time();
	$uploadFile=uploadFile();
	
	//print_r($uploadFile);
	if($uploadFile&&is_array($uploadFile)){
		$arr['face']=$uploadFile[0]['name'];
	}else{
		return "<br/>注册失败";
	}
//	print_r($arr);exit;
	if(insert("stshop_user", $arr)){
		$mes="注册成功!<br/>3秒钟后跳转到登陆页面!<meta http-equiv='refresh' content='3;url=login.php'/>";
	}else{
		$filename="uploads/".$uploadFile[0]['name'];
		if(file_exists($filename)){
			unlink($filename);
		}
		$mes="注册失败!<br/><a href='reg.php'>重新注册</a>|<a href='index.php'>查看首页</a>";
	}
	return $mes;
}
function login(){
	$username=$_POST['username'];
	//addslashes():使用反斜线引用特殊字符
	//$username=addslashes($username);
	$username=mysql_escape_string($username);
	$password=md5($_POST['password']);
	$sql="select * from stshop_user where username='{$username}' and password='{$password}'";
	//$resNum=getResultNum($sql);
	$row=fetchOne($sql);
	//echo $resNum;
	if($row){
		$_SESSION['loginFlag']=$row['id'];
		$_SESSION['username']=$row['username'];
		$mes="登陆成功！<br/><a href='index.php'>3秒钟后跳转到首页</a><meta http-equiv='refresh' content='3;url=index.php'/>";
	}else{
		$mes="登陆失败！<a href='login.php'>重新登陆</a>";
	}
	return $mes;
}

function userOut(){
	$_SESSION=array();
	if(isset($_COOKIE[session_name()])){
		setcookie(session_name(),"",time()-1);
	}
	session_destroy();
	header("location:index.php");
}
/**
 * 修改用户密码
 */
function changePassword(){
    $arr=getUserInfo();
    $id=$arr['id'];
    unset($arr['id']);
    $arr['password']=md5($_POST['newPassword']);
    $result=update("stshop_user", $arr,"id={$id}");
    if($result){
        $response=array('errno'=>0,'errmsg'=>'success','data'=>true,);
    }else{
        $response=array('errno'=>-1,'errmsg'=>'fail','data'=>false,);
    }
    return $response;
}
/**
 * 修改资料
 */
function changeData(){
    $arr=getUserInfo();
    $id=$arr['id'];
    unset($arr['id']);
    if($_POST['sex'])$arr['sex']=$_POST['sex'];
    if($_POST['email'])$arr['email']=$_POST['email'];
    if(update("stshop_user", $arr,"id=$id")){
        $mes="修改成功!<br/>3秒钟后跳转到个人中心!<meta http-equiv='refresh' content='3;url=userHome.php'/>";
    }else{
        $mes="修改失败!<br/><a href='userModify.php'>继续修改</a>|<a href='index.php'>查看首页</a>";
    }
    return $mes;
}
/**
 * 修改头像
 */
function changeFace(){
    $arr=getUserInfo();
    $id=$arr['id'];
    unset($arr['id']);
    $uploadFile=uploadFile();
    if($uploadFile&&is_array($uploadFile)){
        $oldPath=$arr['face'];
        $arr['face']=$uploadFile[0]['name'];
    }else{
        return "<br/>上传失败";
    }
    if(update("stshop_user", $arr,"id=$id")){
        $filename="uploads/".$oldPath;
        if(file_exists($filename)){
            unlink($filename);
        }
        $mes="上传头像成功!<br/>3秒钟后跳转到个人中心!<meta http-equiv='refresh' content='3;url=userHome.php'/>";
    }else{
        $filename="uploads/".$uploadFile[0]['name'];
        if(file_exists($filename)){
            unlink($filename);
        }
        $mes="上传失败!<br/><a href='userModify.php'>重新上传</a>|<a href='index.php'>查看首页</a>";
    }
    return $mes;
}
/**
 * 检测用户是否已登陆.
 */
function checkUserLogined(){
    if($_SESSION['loginFlag']==""){
        alertMes("请先登陆","login.php");
    }
}
function getUserInfo(){
    $userId=intval($_SESSION['loginFlag']);
    $sql="select * from stshop_user where id=$userId";
    $result=fetchOne($sql);
    return $result;
}
function addNewAddress(){
    $arr=$_POST;
    $arr['userId']=$_SESSION['loginFlag'];

    if(insert("stshop_address", $arr)){
        $mes="新增地址成功!<br/><a href='userHome.php'>3秒钟后返回个人中心</a><meta http-equiv='refresh' content='3;url=userHome.php'/>";
    }else{
        $mes="添加地址失败!<br/><a href='javascript:history.go(-1);'>返回上一页</a><meta http-equiv='refresh' content='3;url=javascript:history.go(-1);'/>";
    }
    return $mes;
}
function changeAddress(){
    $arr=$_POST;
    $addressId=$arr['addressId'];
    $where="id=$addressId";
    unset($arr['addressId']);
    if(update("stshop_address", $arr,$where)){
        $mes="修改地址成功!<br/><a href='userHome.php'>3秒钟后返回个人中心</a><meta http-equiv='refresh' content='3;url=userHome.php'/>";
    }else{
        $mes="修改地址失败!<br/><a href='javascript:history.go(-1);'>返回上一页</a><meta http-equiv='refresh' content='3;url=javascript:history.go(-1);'/>";
    }
    return $mes;
}
function deleteAddress(){
    $addressId=$_GET['addressId'];
    $where="id=$addressId";
    $res=delete("stshop_address",$where);
   if($res){
        $response=array('errno'=>0,'errmsg'=>'success','data'=>true,);
    }else{
        $response=array('errno'=>-1,'errmsg'=>'fail','data'=>false,);
    }
    return $response;
}
function getAllAddress(){
    $userId=$_SESSION['loginFlag'];
    $sql="select * from stshop_address where userid=$userId";
    $result=fetchAll($sql);
    return $result;
}
function getAddressById($addressId){
    $sql="select * from stshop_address where id=$addressId";
    $result=fetchOne($sql);
    return $result;
}
function confirmReceipt(){
    $pid=$_POST['pid'];
    $code=$_POST['code'];
    $sql="update stshop_order set Status=3 WHERE code='$code' and pid=$pid";
    $res=fetchOne($sql);
    if(mysql_affected_rows()){
        $response=array('errno'=>0,'errmsg'=>'success','data'=>true,);
    }else{
        $response=array('errno'=>-1,'errmsg'=>'fail','data'=>false,);
    }
    return $response;
}
function getOrderTotal(){
    $userId=$_SESSION['loginFlag'];
    $result=fetchAll("select * from stshop_order where Status=0 and userId=$userId");//未付款的订单条数
    $arr[0]=count($result,COUNT_NORMAL);
    $result=fetchAll("select * from stshop_order where Status=1 and userId=$userId");//未发货的订单条数
    $arr[1]=count($result,COUNT_NORMAL);
    $result=fetchAll("select * from stshop_order where Status=2 and userId=$userId");//待收货的订单条数
    $arr[2]=count($result,COUNT_NORMAL);
    $result=fetchAll("select * from stshop_order where userId=$userId");//所有订单条数
    $arr[3]=count($result,COUNT_NORMAL);
    return $arr;
}